<style scoped>
.mask {
	position:fixed;
	left:0;
	top:0;
	width:100%;
	height:100%;
	background-color:#000;
	opacity:0.3;
	z-index:9;
}
._ReviSion {
	position:fixed;
	top:15%;
	left:50%;
	transform:translateX(-50%);
	width:1220px;
	height:630px;
	z-index:99;
	background-color:white;
	overflow:auto;
}
.RS_top {
	height:40px;
	background:#f8f8f8;
	display:flex;
	justify-content:space-between;
	padding:10px 15px;
}
.RS_top p {
	font-weight:700;
}
.RS_middle {
	width:1193px;
  margin:10px auto;
}
.button_box {
	background:#f8f8f8;
	width:100%;
	height:60px;
	padding:15px 0 15px 15px;
	display:flex;
}
.inp_box2 {
	background:#f8f8f8;
	width:100%;
	height:60px;
	padding:15px 0;
	margin:15px 0;
	display:flex;
}
.inp_box {
	display:flex;
	margin:15px 0 10px 0;
	position:relative;
	left:-28px;
	width:1220px;
}
.inp_box p,.inp_box2 p {
	display:inline-block;
	width:80px;
	text-align:right;
	padding:0 5px 0 10px;
	padding-right:5px;
	line-height:30px;
}
.vrUpdate input:focus {
	outline:none;
	border:1px solid #2a8ffc;
}
.inp {
	border:1px solid #dbdbdb;
	border-radius:3px;
	height:30px;
	padding:0 15px;
}
.inp2 {
	width:160px;
}
.inp3 {
	width:60px;
	margin-right:10px;
}
.inp4 {
	width:400px;
}
.inp5 {
	width:80px;
	margin-right:10px;
}
.inp6 {
	width:170px;
}
.RS_button {
	margin-left:1070px;
}
.mg {
	margin-left:32px;
}
</style>

<style>
.vrUpdate ._ReviSion .inp_box .el-input__inner {
	height:30px;
	width:160px;
}
.vrUpdate ._ReviSion .inp_box .sty .el-input__inner {
	height:30px!important;
	width:100px;
}
.vrUpdate ._ReviSion .inp_box .sty2 .el-input__inner {
	height:30px;
	width:160px;
}
.vrUpdate ._ReviSion .inp_box .sty3 .el-input__inner {
	height:30px!important;
	width:170px;
}
.vrUpdate ._ReviSion .RS_middle .inp_box2 .el-input__inner {
	height:30px;
	width:160px;
}
.vrUpdate ._ReviSion .el-checkbox.is-bordered {
	padding:4px 20px 9px 8px;
	height:30px;
	width:70px;
}
</style>

<template>
  <div class="vrUpdate">
    <div class="mask"></div>
    <div class="_ReviSion">
      <div class="RS_top">
        <p>改单(厂商退货)</p>
        <i @click="closeReviSion" class="el-icon-close"></i>
      </div>
      <div class="RS_middle">
        <div class="button_box">
          <el-button type="primary" icon="el-icon-search" @click="search">查询</el-button>
          <el-button icon="el-icon-edit-outline" @click="modification(0)">修改</el-button>
          <!-- <el-button icon="el-icon-edit-outline" @click="() => {this.isNew = true}">new</el-button> -->
        </div>
        <div class="inp_box">
          <p>单号</p>
          <input class="inp inp2" type="text" v-model="odd" :disabled="changeNo">
          <p>退货日期</p>
          <el-date-picker disabled class="inp2" v-model="inputDate" type="date" value-format="yyyy-MM-dd HH:mm:ss" placeholder="选择日期"></el-date-picker>
          <p>货号</p>
          <div class="sty2">
            <el-input v-model="kindsNumber" disabled></el-input>
          </div>
          <p>仓库</p>
          <input class="inp inp3" type="text" v-model="companyNo" @blur="companyNoBlur" :disabled="!isNew">
          <div class="sty">
            <el-select v-model="companyName" @change="warehouseChange" :disabled="!isNew">
              <el-option v-for="item in warehouseList" :key="item.value" :label="item.label" :value="item.value"></el-option>
            </el-select>
          </div>
          <p>厂商编号</p>
          <input class="inp inp3" type="text" v-model="manufacturerNo" @blur="manufacturerNoBlur" :disabled="lockNo || !isNew">
          <div class="sty">
            <el-select v-model="manufacturerName" @change="manufacturerChange" :disabled="lockNo || !isNew">
              <el-option v-for="item in manufacturerList" :key="item.value" :label="item.label" :value="item.value"></el-option>
            </el-select>
          </div>
        </div>
        <div class="inp_box">
          <p>售价</p>
          <div class="sty2">
            <el-input v-model="sellingPrice" disabled></el-input>
          </div>
          <p>厂退序号</p>
          <div class="sty2">
            <el-input v-model="inputSequence" disabled></el-input>
          </div>
          <p>进厂实价</p>
          <input class="inp inp5" type="text" v-model="onePrice" :disabled="!checked || changeNo" @blur="onePriceBlur">
          <el-checkbox v-model="checked" label="选择" border></el-checkbox>
          <p>数量</p>
          <input class="inp inp6" type="text" v-model="sum" :disabled="isRFID || changeNo" @blur="sumBlur">
          <p>配货方式</p>
          <div class="sty3">
            <el-select v-model="cooperateWay" :disabled="changeNo">
              <el-option v-for="item in cooperateWayList" :key="item.value" :label="item.label" :value="item.value"></el-option>
            </el-select>
          </div>
        </div>
        <div class="inp_box">
          <p>备注</p>
          <input class="inp inp4" type="text" v-model="remark" :disabled="changeNo"></div>
        <div class="inp_box2">
          <p style="width:55px">货号</p>
          <div class="sty2">
            <el-input v-model="searchKindsNumber" @keyup.enter.native="searchKN"></el-input>
          </div>
          <p>厂退序号</p>
          <div class="sty2">
            <el-input v-model="searchInputSequence" @keyup.enter.native="search"></el-input>
          </div>
        </div>
        <el-table height="250" :data="tableData" highlight-current-row :header-cell-style="{background:'#f4f4f4',padding:0,height:'40px'}" :cell-style="{padding:0,height:'36px'}" style="width: 100%" @row-click="rowClick">
          <el-table-column type="index" label="序号" width="50"></el-table-column>
          <el-table-column prop="sequence" label="厂退序号" width="160"></el-table-column>
          <el-table-column prop="odd" label="单号" width="100"></el-table-column>
          <el-table-column prop="manufacturerNo" label="厂商编号" width="80"></el-table-column>
          <el-table-column prop="warehouseNo" label="仓库编号" width="80"></el-table-column>
          <el-table-column prop="kindsNumber" label="货号" width="160"></el-table-column>
          <el-table-column prop="colorNameNo" label="色码" width="80"></el-table-column>
          <el-table-column prop="sizeNo" label="尺码编号" width="80"></el-table-column>
          <el-table-column prop="sum" label="数量" width="80"></el-table-column>
          <el-table-column prop="sellingPrice" label="售价" width="80"></el-table-column>
          <el-table-column prop="sellingPriceMoney" label="售价金额" width="100"></el-table-column>
          <el-table-column prop="onePrice" label="单价" width="100"></el-table-column>
          <el-table-column prop="money" label="金额" width="100"></el-table-column>
        </el-table>
      </div>
      <!-- <el-button @click="closeReviSion" class="RS_button">取消</el-button>
      <el-button type="primary" @click="modification(1)">保存</el-button> -->
    </div>
  </div>
</template>

<script>
import { outQuery, outModifySequence, renewRepertory } from '../../../../network/manufacturer'
import today from '../../../../components/mixins/today'

import exportMethods from '../../../../components/mixins/exportMethods'

export default {
  mixins: [today],
  props: {
    selectInputSequence: {
      type: String,
      default: ''
    },
    manufacturerL: {
      type: Array,
      default: []
    },
    warehouseL: {
      type: Array,
      default: []
    },
    showReviSion: {
      type: String,
      default: 'none'
    },
    isRFID: {
      type: Boolean,
      default: false
    },
    Str: {
      type: String,
      default: ''
    }
  },
  data(){
    return{
      inputDate: '',
      odd: '',
      kindsNumber: '',
      companyNo: '',
      companyName: '',
      warehouseList: [],
      manufacturerNo: '',
      manufacturerName: '',
      manufacturerList: [],
      sellingPrice: '',
      inputSequence: '',
      onePrice: '',
      checked: false,
      
      changeNo: false,
      lockNo: false,
      sum: '',
      cooperateWay: '',
      cooperateWayList: [
        {value:'配货',label:'配货'},
        {value:'订货',label:'订货'},
        {value:'订断',label:'订断'},
        {value:'追断',label:'追断'}
      ],
      remark: '',

      searchKindsNumber: '',
      searchInputSequence: '',

      tableData: [],
      updateData: {},
      isNew: true
    }
  },
  watch: {
    selectInputSequence(a,b){
      this.searchInputSequence = a
    },
    showReviSion(a,b){
      if (a == 'block' && this.searchInputSequence) {
        this.search()
      }
    },
    manufacturerL(a,b){
      this.manufacturerList = a
    },
    warehouseL(a,b){
      this.warehouseList = a
    },
    odd(a,b){
      this.odd = a.toUpperCase().trim()
      if (this.allData.length && a != this.allData[0].odd && !this.lockNo) {
        this.lockNo = true
      }
      this.allData.forEach(e => {
        e.receiptsNumber = a
        e.odd = a
      })
      this.$forceUpdate()
    },
    // inputDate(a,b){
    //   let tmp = a.slice(0,10)
    //   if (this.allData.length && tmp != this.allData[0].purchaseTime && !this.lockNo) {
    //     this.lockNo = true
    //   }
    //   this.tableData.forEach(e => {
    //     e.purchaseTime = a
    //     // e.timeShort = a.slice(0,10)
    //   })
    // },
    inputWarehouseNo(a,b){
      this.inputWarehouseNo = a.toUpperCase().trim()
    },
    companyNo(a,b){
      this.companyNo = a.toUpperCase().trim()
      if (this.allData.length && a != this.allData[0].warehouseNo && !this.lockNo) {
        this.lockNo = true
      }
      this.allData.forEach(e => {
        e.warehouseNo = a
      })
    },
    cooperateWay(a,b){
      if (this.allData.length && a != this.allData[0].cooperateWay && !this.lockNo) {
        this.lockNo = true
      }
      this.allData.forEach(e => {
        e.cooperateWay = a
      })
    },
    remark(a,b){
      if (this.allData.length && a != this.allData[0].remark && !this.lockNo) {
        this.lockNo = true
      }
      this.allData.forEach(e => {
        e.remark = a
      })
    },
    manufacturerNo(a,b){
      this.manufacturerNo = a.toUpperCase().trim()
    },
    searchKindsNumber(a,b){
      this.searchKindsNumber = a.toUpperCase().trim()
    },
    searchInputSequence(a,b){
      this.searchInputSequence = a.toUpperCase().trim()
    }
  },
  methods:{
    clearData(){
      this.odd = ''
      this.inputWarehouseName = ''
      this.companyNo = ''
      this.companyName = ''
      this.manufacturerNo = ''
      this.manufacturerName = ''
      this.inputDate = ''
      this.kindsNumber = ''
      this.sequence = ''
      this.onePrice = ''
      this.sum = ''
      this.cooperateWay = '配货'
      this.searchKindsNumber = ''
      // this.searchInputSequence = ''
      this.tableData = []
      this.allData = []
      this.lockNo = false
      this.changeNo = false
      // this.isNew = false
    },
    closeReviSion(){
      this.$emit('closeReviSion')
      this.clearData()
    },

    // 厂商编号，仓库，失去焦点
    manufacturerNoBlur(){
      if (this.manufacturerNo != '') {
        for (const i of this.manufacturerList) {
          if (this.manufacturerNo == i.value) {
            this.manufacturerName = i.label
            if (this.allData.length && this.manufacturerNo != this.allData[0].manufacturerNo) {
              // console.log(this.manufacturerNo)
              this.changeNo = true
              this.allData.forEach(e => {
                e.manufacturerNo = this.manufacturerNo
              })
            }
            return
          }
        }
        this.$alert('无此厂商编号', '', {
          confirmButtonText: '确定'
        })
        this.manufacturerNo = ''
        this.manufacturerName = ''
      }
    },
    companyNoBlur(){
      if (this.companyNo != '') {
        for (const i of this.warehouseList) {
          if (this.companyNo == i.value) {
            this.companyName = i.label
            if (this.allData.length && this.companyNo != this.allData[0].warehouseNo) {
              // console.log(this.manufacturerNo)
              this.lockNo = true
              this.allData.forEach(e => {
                e.warehouseNo = this.companyNo
              })
            }
            return
          }
        }
        this.$alert('无此仓库编号', '', {
          confirmButtonText: '确定'
        })
        this.companyNo = ''
        this.companyName = ''
      }
    },
    onePriceBlur(){
      let a = this.onePrice
      let r = /^[1-9]\d*(.[0-9]{1,2})?$/
      // console.log(r.test(a))
      if (!r.test(a)) {
        this.$alert('实收单价请输入数字，小数不得超过两位', '', {
          confirmButtonText: '确定'
        })
        this.onePrice = this.updateData.onePrice
        return
      }
      if (this.allData.length && a != this.allData[0].onePrice && !this.lockNo) {
        this.lockNo = true
      }
      this.updateData.onePrice = a
      // console.log(a)
      this.updateData.money = (this.updateData.sum * this.updateData.onePrice).toFixed(2)
    },
    sumBlur(){
      let a = this.sum
      let r = /^(0|[1-9]\d*)$/
      if (!r.test(a)) {
        this.$alert('数量请输入整数', '', {
          confirmButtonText: '确定'
        })
        this.sum = this.updateData.sum
        return
      }
      if (this.tableData.length && a != this.tableData[0].sum && !this.lockNo) {
        this.lockNo = true
      }
      this.updateData.sum = a
      this.updateData.money = (this.updateData.sum * this.updateData.onePrice).toFixed(2)
    },
    warehouseChange(e){
      this.companyNo = e
      if (this.allData.length && this.companyNo != this.allData[0].warehouseNo && !this.lockNo) {
        this.lockNo = true
        this.tableData.forEach(e => {
          e.warehouseNo = this.companyNo
        })
      }
    },
    manufacturerChange(e){
      this.manufacturerNo = e
      if (this.allData.length && this.manufacturerNo != this.allData[0].manufacturerNo) {
        this.changeNo = true
        this.allData.forEach(e => {
          e.manufacturerNo = this.manufacturerNo
        })
      }
    },

    fillBlanks(row){
      this.inputSequence = row.sequence
      this.inputDate = row.purchaseTime.slice(0,10) + ' 12:00:00'
      this.manufacturerNo = row.manufacturerNo
      this.odd = row.receiptsNumber || ''
      this.companyNo = row.warehouseNo
      this.cooperateWay = row.cooperateWay
      this.remark = row.remark

      this.manufacturerNoBlur()
      this.companyNoBlur()
    },
    rowClick(row){
      // console.log(row)
      this.updateData = row
      
      this.kindsNumber = row.kindsNumber
      
      this.sellingPrice = row.sellingPrice
      this.onePrice = row.onePrice
      this.sum = row.sum
    },

    // 查询
    search(){
      if (!this.searchInputSequence) {
        this.$alert('请输入厂进序号查询', '', {
          confirmButtonText: '确定'
        })
        return
      }
      outQuery(JSON.stringify({
        sequence: this.searchInputSequence,
        pageData: {
          page: 1,
          limit: 999
        }
      })).then(res => {
        if (res.data.code == 200) {
          let obj = res.data.data.resultList[0]
          this.allData = obj.detailVOList
          this.allData.forEach(e => {
            e.knInfo = e.kindsNumber
            e.kindsNumber = e.knInfo.kindsNumber
            e.colorNameNo = e.colorSize
            e.colorName = e.knInfo.colorName
            e.colorNo = e.knInfo.colorNo
            e.size = e.knInfo.size
            e.sizeNo = e.knInfo.sizeNo
            e.commodityName = e.knInfo.commodityName
            e.brandedName = e.knInfo.brandedName
            
            e.purchaseTime = obj.purchaseTime.slice(0,10)
            e.enteringTime = e.enteringTime
            e.initialSum = e.sum
          })
          this.tableData = this.allData
          this.fillBlanks(obj)
        }
      })
    },

    modification(f){
      if (!this.companyNo) {
        this.$alert('仓库不能为空', '', {
          confirmButtonText: '确定'
        })
        return
      }
      if (!this.manufacturerNo) {
        this.$alert('厂商不能为空', '', {
          confirmButtonText: '确定'
        })
        return
      }
      if (!this.inputDate) {
        this.$alert('退货日期不能为空', '', {
          confirmButtonText: '确定'
        })
        return
      }

      let kindsNumberNumberMap = {}
      this.allData.forEach(e => {
        e.flagToken = this.Str
        if (e.initialSum != e.sum) {
          kindsNumberNumberMap[e.kindsNumber] = e.sum - e.initialSum
        }
      });
      if (JSON.stringify(this.detailRow) == "{}") {
        renewRepertory(JSON.stringify({
          manufacturerNo: this.updateData.manufacturerNo,
          companyNo: this.companyNo,
          kindsNumberNumberMap
        })).then(res => {
          if (res.data.code == 200) {
            if (!res.data.data) {
              outModifySequence(JSON.stringify(this.allData)).then(res => {
                if (res.data.code == 200) {
                  this.$message({
                    message: '修改成功',
                    type: 'success'
                  })
                  if (f) {
                    this.closeReviSion()
                  }
                } else {
                  this.$message.error('修改失败')
                }
              })
            } else {
              this.$alert(String(res.data.data) + '库存不足', '', {
                confirmButtonText: '确定'
              })
            }
          }
        })
        return
      }

      outModifySequence(JSON.stringify(this.allData)).then(res => {
        if (res.data.code == 200) {
          this.$message({
            message: '修改成功',
            type: 'success'
          })
          if (f) {
            this.closeReviSion()
          }
        } else {
          this.$message.error('修改失败')
        }
      })
    },

    searchKN(){
      if (!this.searchKindsNumber) return
      this.tableData = []
      this.allData.forEach(e => {
        if (this.searchKindsNumber == e.kindsNumber) {
          this.tableData.push(e)
        }
      })
    }
  }
}
</script>